{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import xmltodict\n",
    "import os\n",
    "import numpy as np\n",
    "import cv2\n",
    "\n",
    "xml_path = '。/results.xml'\n",
    "sample_path = './sample'\n",
    "result_path = './result'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(xml_path, 'r') as file:\n",
    "        data = file.read().replace('\\n', '')\n",
    "        \n",
    "xml_dict=xmltodict.parse(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(xml_dict['results']['image'][0]['object']['bndbox'])\n",
    "print(xml_dict['results']['image'][0]['filename'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def bb_intersection_over_union(boxA, boxB):\n",
    "    # determine the (x, y)-coordinates of the intersection rectangle\n",
    "    xA = max(boxA[0], boxB[0])\n",
    "    yA = max(boxA[1], boxB[1])\n",
    "    xB = min(boxA[2], boxB[2])\n",
    "    yB = min(boxA[3], boxB[3])\n",
    " \n",
    "    # compute the area of intersection rectangle\n",
    "    interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)\n",
    " \n",
    "    # compute the area of both the prediction and ground-truth\n",
    "    # rectangles\n",
    "    boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)\n",
    "    boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)\n",
    "    \n",
    "    # compute the intersection over union by taking the intersection\n",
    "    # area and dividing it by the sum of prediction + ground-truth\n",
    "    # areas - the interesection area\n",
    "    iou = interArea / float(boxAArea + boxBArea - interArea)\n",
    " \n",
    "    # return the intersection over union value\n",
    "    return iou\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "iou_list = []\n",
    "detected = 0\n",
    "for image_result in xml_dict['results']['image']:\n",
    "    filename = image_result['filename']\n",
    "    bbox = [image_result['object']['bndbox']['xmin'],image_result['object']['bndbox']['ymin'],image_result['object']['bndbox']['xmax'],image_result['object']['bndbox']['ymax']]\n",
    "    bbox = [int(float(box)) for box in bbox]\n",
    "    bbox = np.array(bbox, dtype = np.float32)   \n",
    "    label_path = os.path.join(sample_path, filename.split('.jpg')[0] + '.xml')\n",
    "    with open(label_path, 'r') as file:\n",
    "        data = file.read().replace('\\n', '')\n",
    "    xml=xmltodict.parse(data)\n",
    "    xmax = int(xml['annotation']['object']['bndbox']['xmax'])\n",
    "    xmin = int(xml['annotation']['object']['bndbox']['xmin'])\n",
    "    ymax = int(xml['annotation']['object']['bndbox']['ymax'])\n",
    "    ymin = int(xml['annotation']['object']['bndbox']['ymin'])\n",
    "    xcenter = (xmax + xmin) / 2\n",
    "    ycenter = (ymax + ymin) / 2\n",
    "    xlen = xmax - xmin\n",
    "    ylen = ymax - ymin   \n",
    "    target = np.array([xmin,ymin,xmax,ymax], dtype = np.float32)   \n",
    "    \n",
    "    \n",
    "    iou = bb_intersection_over_union(bbox,target)#bbox_iou(bbox.reshape(1,4),target.reshape(1,4),False)\n",
    "    print(filename)\n",
    "    print('bbox',bbox)\n",
    "    print('target',target)\n",
    "    print('iou',iou)\n",
    "    iou_list.append(iou)\n",
    "\n",
    "    img = cv2.imread(os.path.join(sample_path, filename), cv2.IMREAD_COLOR)\n",
    "    cv2.rectangle(img, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (0,255,0), 2)\n",
    "    cv2.imwrite(os.path.join(result_path, filename), img)\n",
    "\n",
    "    if iou>0.5:\n",
    "        detected += 1\n",
    "\n",
    "print('iou',np.mean(iou_list))\n",
    "print('detected',detected)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
